<?php

/**
 * This is the model class for table "diary_log".
 *
 * The followings are the available columns in table 'diary_log':
 * @property integer $id
 * @property string $owner_id
 * @property integer $website_id
 * @property string $website
 * @property integer $totalclick
 * @property integer $validclick
 * @property integer $joinmb
 * @property integer $vjoinmb
 * @property string $valid_ordernum
 * @property string $firstordernum
 * @property string $valid_firstordernum
 * @property integer $ordernum
 * @property string $orderamount
 * @property integer $dayweek
 * @property string $yearweek
 * @property string $usertype
 * @property string $adddate
 */
class DiaryLog extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return DiaryLog the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'diary_log';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('website_id, website', 'required'),
			array('website_id, totalclick, validclick, joinmb, vjoinmb, ordernum, check_ordernum, check_joinmb, check_firstordernum, dayweek', 'numerical', 'integerOnly'=>true),
			array('owner_id, website', 'length', 'max'=>50),
			array('valid_ordernum, firstordernum, valid_firstordernum, orderamount,firstorderamount, check_orderamount, check_firstorderamount', 'length', 'max'=>10),
			array('yearweek', 'length', 'max'=>20),
			array('usertype', 'length', 'max'=>8),
			array('adddate', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, owner_id, website_id, website, totalclick, validclick, joinmb, vjoinmb, valid_ordernum, firstordernum, valid_firstordernum, ordernum, orderamount, dayweek, yearweek, usertype, adddate', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'owner_id' => 'Owner',
			'website_id' => 'Website',
			'website' => 'Website',
			'totalclick' => 'Totalclick',
			'validclick' => 'Validclick',
			'joinmb' => 'Joinmb',
			'vjoinmb' => 'Vjoinmb',
			'valid_ordernum' => 'Valid Ordernum',
			'firstordernum' => 'Firstordernum',
			'firstorderamount' => 'First Order Amount',
			'valid_firstordernum' => 'Valid First Order Count',
			'valid_firstorderamount' => 'Valid Firstorder Amount',
			'valid_orderamount' => 'Valid First Order Amount',
			'check_ordernum' => 'Check Order Count',
			'check_firstordernum' => 'Check First Order Count',
			'check_joinmb' => 'Check Members',
			'check_orderamount' => 'Check Order Amount',
			'check_firstorderamount' => 'Check First Order Amount',
			'ordernum' => 'Ordernum',
			'orderamount' => 'Orderamount',
			'dayweek' => 'Dayweek',
			'yearweek' => 'Yearweek',
			'usertype' => 'Usertype',
			'adddate' => 'Adddate',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('owner_id',$this->owner_id,true);
		$criteria->compare('website_id',$this->website_id);
		$criteria->compare('website',$this->website,true);
		$criteria->compare('totalclick',$this->totalclick);
		$criteria->compare('validclick',$this->validclick);
		$criteria->compare('joinmb',$this->joinmb);
		$criteria->compare('vjoinmb',$this->vjoinmb);
		$criteria->compare('valid_ordernum',$this->valid_ordernum,true);
		$criteria->compare('firstordernum',$this->firstordernum,true);
		$criteria->compare('firstorderamount',$this->firstorderamount,true);
		$criteria->compare('valid_firstordernum',$this->valid_firstordernum,true);
		$criteria->compare('valid_firstorderamount',$this->valid_firstorderamount,true);
		$criteria->compare('valid_orderamount',$this->valid_orderamount,true);
		$criteria->compare('check_firstordernum',$this->check_firstordernum,true);
		$criteria->compare('check_ordernum',$this->check_ordernum,true);
		$criteria->compare('check_orderamount',$this->valid_firstordernum,true);
		$criteria->compare('check_firstorderamount',$this->valid_firstordernum,true);
		$criteria->compare('check_joinmb',$this->check_joinmb,true);
		$criteria->compare('ordernum',$this->ordernum);
		$criteria->compare('orderamount',$this->orderamount,true);
		$criteria->compare('dayweek',$this->dayweek);
		$criteria->compare('yearweek',$this->yearweek,true);
		$criteria->compare('usertype',$this->usertype,true);
		$criteria->compare('adddate',$this->adddate,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public static function load($owner_id, $website_id, $date=null) {
		if ($date == null) $date = date('Y-m-d');
		$params = array(':owner_id' => $owner_id, ':adddate' => $date, ':website_id' => $website_id);
		$model = DiaryLog::model()->find('owner_id=:owner_id AND adddate=:adddate AND website_id=:website_id', $params);
		$website = Website::model()->findByPk($website_id);
		if (empty($model)) {
			$model = new self();
			$model->owner_id = $owner_id;
			$model->website_id = $website_id;
			$model->website = $website->domain;
			$model->adddate = $date;
			$d = getdate(strtotime($date));
			$model->dayweek = $d['wday'];
			$model->yearweek = ceil($d['yday']/7);
			$model->usertype = '0';
			$model->save();
		}
		return $model;
	}
}